(function($){
	$.fn.suggest=function(options){
		var settings = {
			'resultsClass': 'sg_results',
			'sLength' : 1, //关键词最低长度
			'matchClass' : 'red',
			'ajaxurl' : '' //Ajax地址
		};
		if(options){
			$.extend(settings, options);
		}
		
		this.each(function(){
			var $input=$(this);
			var $results = $(document.createElement("ul"));
			var InvalueO=''; //上次检索值
			var iPoint=-1; //提示的指针
			var cache = [];	
			$results.addClass(settings.resultsClass);//.appendTo('body');
			$input.after($results);
			
			$input.attr("autocomplete", "off").live('blur',function() {
				setTimeout(function() {
					$results.hide()
				}, 200);
			}).live('keyup',function(e){
				var intKeyCode=e.keyCode,Invalue=$.trim($input.val());
				if(intKeyCode==13 || intKeyCode==9){
					selectCurrentResult();
					return false;	
				}				
				if ((/27$|38$|40$/.test(intKeyCode) && $results.is(':visible'))) {
					switch(intKeyCode) {	
						case 38: // up
							prevResult();
							break;				
						case 40: // down
							nextResult();
							break;
						case 27: //	escape
							$results.hide();
							break;
					}
				}else if (Invalue != InvalueO) {
					InvalueO = Invalue;					
					suggest();
				}			
			}).live('keydown',function(e){
				var intKeyCode=e.keyCode,Invalue=$.trim($input.val());
				if(intKeyCode==9 || intKeyCode==13){
					selectCurrentResult();
					return false;
				}
			});
			
			function selResult(){
				$currentResult = getCurrentResult();
				if($currentResult){
					$input.val($currentResult.text());
					$input.siblings('input:hidden').val($currentResult.attr('vid'))
				}else{
					$input.val(InvalueO);
					$input.siblings('input:hidden').val(0)
				}
			}
			
			function nextResult() {
				$currentResult = getCurrentResult();
				if ($currentResult)
					$currentResult.removeClass(settings.selectClass).next().addClass(settings.selectClass);
				else
					$results.children('li:first-child').addClass(settings.selectClass);
				selResult();
			}
			function prevResult() {
				$currentResult = getCurrentResult();
				if ($currentResult)
					$currentResult.removeClass(settings.selectClass).prev().addClass(settings.selectClass);
				else
					$results.children('li:last-child').addClass(settings.selectClass);
				selResult();
			}
			function selectCurrentResult() {
				$currentResult = getCurrentResult();
				if ($currentResult) {
					$input.val($currentResult.text());
					$results.hide();					
				}
				return false;
			}
			
			//查询数据
			function suggest() {
				$input.siblings('input:hidden').val(0);
				if (InvalueO.length>= settings.sLength) {
					cached = checkCache();
					if (cached) {
						displayItems(cached['items']);
					} else {
						$.get(settings.ajaxurl, {q:InvalueO}, function(data) {
							if(data.error>0 || data.content==''){
								$results.hide();
								return false;
							}
							var items = parseTxt(data.content);
							displayItems(items);
							addToCache(InvalueO,items);
						},"json");
					}
				} else {
					$results.hide();
				}
			}
			
			function displayItems(items) {
				var html = '';
				for (var i = 0; i < items.length; i++)
					html += '<li vid="'+items[i].id+'">' + items[i].name + '</li>';
				if(items.length>10){
					$results.addClass('fsuggest-scroll');	
				}
				$results.html(html).show();
			}
			
			
			//检查是否缓存
			function checkCache() {
				for (var i = 0; i < cache.length; i++)
					if (cache[i]['name'] ==InvalueO) {
						cache.unshift(cache.splice(i,1)[0]);
						return cache[0];
				}				
				return false;			
			}
			function addToCache(name, items) {
				cache.push({
					name: name,
					items: items
				});
			}

			function parseTxt(items) {
				// parse returned data for non-empty items
				for (var i = 0; i < items.length; i++) {
					name = items[i].name.replace(
							new RegExp(InvalueO, 'ig'), 
							function(InvalueO) { return '<span class="' + settings.matchClass + '">' + InvalueO + '</span>' }
							);
						items[i].name = name;
				}
				return items;
			}
			
			//当前结果
			function getCurrentResult() {
				if (!$results.is(':visible'))
					return false;
				var $currentResult = $results.children('li.' + settings.selectClass);
				if (!$currentResult.length)
						$currentResult = false;
				return $currentResult;
			}

		});	
	}
	
})(jQuery);